﻿using DAL;
using DAL.Entities;
using DAL.FKTZ;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BLL.FKTZ
{
    public class FktzBLL
    {
        private readonly FktzDataContext _dataContext;
        private readonly FktzDAL _fktzDALDAL;
        private readonly FktzAttachmentDAL _attachmentDAL;

        public FktzBLL()
        {
            _dataContext = new FktzDataContext();
            _fktzDALDAL = new FktzDAL(_dataContext);
            _attachmentDAL = new FktzAttachmentDAL(_dataContext);

        }
        public async Task<IEnumerable<FktzModel>> ListAsync(string deptid,string name, DateTime? startTime, DateTime? endTime, int pageSize, int pageIndex)
        {
            var data = await _fktzDALDAL.ListAsync(deptid,name,startTime,endTime,pageSize, pageIndex);
            return data.Select(x => new FktzModel() { FktzId = x.FktzId, EntryName = x.EntryName,DeptId = x.DeptId, UserName = x.UserName, ContactInformation = x.ContactInformation, UserIDNumber = x.UserIDNumber, VisitinUnit = x.VisitinUnit, Cause = x.Cause, AccessTime = x.AccessTime, Temperature = x.Temperature, Remarks = x.Remarks});
        }
        public async Task<object> ListAsync()
        {
            var data = await _fktzDALDAL.ListAsync();
            return data.Count();
        }
        public async Task CreateAsync(FktzModel model)
        {
            var entity = new Fktz()
            {
                FktzId = model.FktzId,
                EntryName = model.EntryName,
                DeptId=model.DeptId,
                UserName = model.UserName,
                ContactInformation = model.ContactInformation,
                UserIDNumber = model.UserIDNumber,
                VisitinUnit = model.VisitinUnit,
                Cause = model.Cause,
                AccessTime = model.AccessTime,
                Temperature = model.Temperature,
                Remarks = model.Remarks

            };
            var attachmentEntities = new List<FktzAttachment>();
            if (model.Attachments != null)
            {
                foreach (var item in model.Attachments)
                {
                    attachmentEntities.Add(new FktzAttachment()
                    {
                        AttachmentId = item.AttachmentId,
                        FileName = item.FileName,
                        FileStream = item.FileStream,
                        Extension = item.Extension,
                        MediaType = item.MediaType,
                        Option = "",
                        BusinessId = model.FktzId
                    });
                }
            }
            //if (model.EmployeeImage!=null)
            //{
            //    FktzAttachment employeeImage = new FktzAttachment()
            //    {
            //        AttachmentId = model.EmployeeImage.AttachmentId,
            //        FileName = model.EmployeeImage.FileName,
            //        FileStream = model.EmployeeImage.FileStream,
            //        Extension = model.EmployeeImage.Extension,
            //        MediaType = model.EmployeeImage.MediaType,
            //        Option = "",
            //        BusinessId = model.FktzId
            //    };
            //    await _attachmentDAL.CreateAsync(employeeImage);
            //    await _attachmentDAL.SaveChangeAsync();
            //}
            await _fktzDALDAL.CreateAsync(attachmentEntities);
            await _fktzDALDAL.CreateAsync(entity);
            await _fktzDALDAL.SaveChangeAsync();
        }
        public string Save(FktzModel entity)
        {
            try
            {
                return _fktzDALDAL.Save(entity);
            }
            catch (Exception e)
            {

                return e.ToString();
            }
        }

        public async Task DeleteAsync(string id)
        {
            await _fktzDALDAL.DeleteAsync(id);
            await _fktzDALDAL.SaveChangeAsync();
        }

        public async Task UpdateAsync(FktzModel model)
        {
            var entity = await _fktzDALDAL.GetAsync(model.FktzId);
            entity.FktzId = model.FktzId;
            entity.EntryName = model.EntryName;
            entity.DeptId = model.DeptId;
            entity.UserName = model.UserName;
            entity.ContactInformation = model.ContactInformation;
            entity.UserIDNumber = model.UserIDNumber;
            entity.VisitinUnit = model.VisitinUnit;
            entity.Cause = model.Cause;
            entity.AccessTime = model.AccessTime;
            entity.Temperature = model.Temperature;
            entity.Remarks = model.Remarks;
            
            await _fktzDALDAL.UpdateAsync(entity);
            await _fktzDALDAL.SaveChangeAsync();
        }

        public async Task<FktzModel> GetAsync(string id)
        {
            var entity = await _fktzDALDAL.GetAsync(id);
            if (entity == null)
            {
                return null;
            }
            //var attachmentEntity = (await _attachmentDAL.ListAsync(id, "photo")).FirstOrDefault();
            //FktzAttachment attachmentEntity = new FktzAttachment();
            return new FktzModel()
            {
                FktzId = entity.FktzId,
                EntryName = entity.EntryName,
                UserName = entity.UserName,
                ContactInformation = entity.ContactInformation,
                UserIDNumber = entity.UserIDNumber,
                VisitinUnit = entity.VisitinUnit,
                Cause = entity.Cause,
                AccessTime = entity.AccessTime,
                Temperature = entity.Temperature,
                Remarks = entity.Remarks
               // EmployeeImage = attachmentEntity == null ? null : new FktzAttachmentModel() { AttachmentId = attachmentEntity.AttachmentId, Extension = attachmentEntity.Extension, FileName = attachmentEntity.FileName, MediaType = attachmentEntity.MediaType },

            };
        }
    }
}
